package com.class04;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/27 20:14
 */
public class LambdaTest1 {
    //    **语法格式一：**无参，无返回值
    @Test
    public void test1() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("你好");
            }
        };
        r1.run();
        System.out.println("*********************************");
        Runnable r2 = () -> {
            System.out.println("好的");
        };
        r2.run();


    }

    //    **语法格式二：**Lambda 需要一个参数，但是没有返回值。
    public void test02() {
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String o) {
                System.out.println(o);
            }
        };
        System.out.println("===========================");
        Consumer<String> con1 = (String s) -> {
            System.out.println("s = " + s);
        };
        con1.accept("哈喽");
    }

    //**语法格式三：**数据类型可以省略，因为可由编译器推断得出，称为“类型推断”
    @Test
    public void test3() {
        Consumer<String> con1 = (String s) -> {
            System.out.println("s = " + s);
        };
        con1.accept("如果你是土财主");
        System.out.println("===========================");
        Consumer<String> con2 = (s) -> {
            System.out.println("s = " + s);
        };
        con2.accept("一切家务我全包");

    }

    //    **语法格式四：**Lambda 若只需要一个参数时，参数的小括号可以省略
    @Test
    public void test() {
        Consumer<String> con2 = (s) -> {
            System.out.println("s = " + s);
        };
        con2.accept("一切家务我全包");
        System.out.println("===========================");
        Consumer<String> con3 = s -> System.out.println("s = " + s);
        con3.accept("你真的好逗");
    }

    //    **语法格式五：**Lambda 需要两个或以上的参数，多条执行语句，并且可以有返回值
    @Test
    public void test05() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println("o1 = " + o1);
                System.out.println("o2 = " + o2);
                return Integer.compare(o1, o2);
            }
        };
        System.out.println("com.compare(12,21) = " + com.compare(12, 21));
        System.out.println("==========================================");
        Comparator<Integer> com2 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return Integer.compare(o1, o2);
        };
        System.out.println("com.compare(12,21) = " + com.compare(12, 21));

    }

    //**语法格式六：**当 Lambda 体只有一条语句时，return 与大括号若有，都可以省略
    @Test
    public void test7() {
        Comparator<Integer> com2 = (o1, o2) -> {
            return o1.compareTo(o2);
        };
        System.out.println("com2.compare(12,21) = " + com2.compare(12, 21));
        System.out.println("============================");
        Comparator<Integer> com = (o1, o2) -> o1.compareTo(o2);
        System.out.println(com.compare(12, 21));
    }

    @Test
    public void test8() {
        love(500, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("给我女朋友买了一束花，花了：" + aDouble);
            }
        });
        love(300, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("给我大哥买了支笔，花了：" + aDouble);
            }
        });
    }

    public void love(double money, Consumer<Double> con) {
        con.accept(money);
    }

    @Test
    public void test09() {
        List<String> list = Arrays.asList("天津", "北京", "南京", "东京", "西京", "黄京");

        List<String> filterString = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterString);
        List<String> filterString1 = filterString(list, s -> s.contains("京"));
        System.out.println("filterString1 = " + filterString1);

    }

    //    根据给定的规则，过滤集合中的字符串，此规则由Predicate的方法决定
    public List<String> filterString(List<String> list, Predicate<String> pre) {
        ArrayList<String> filterList = new ArrayList<>();
        for (String s : list) {
            if (pre.test(s)) {
                filterList.add(s);
            }
        }
        return filterList;
    }




}
